//
//  HSYAlertView.m
//  TestHSYAlertView
//
//  Created by Sean on 16/1/15.
//  Copyright (c) 2016年 seanhuang1661. All rights reserved.
//

#import "HSYAlertView.h"
#import "UIColor+FY.h"
#import "UIView+FY.h"
#import "UIImage+FY.h"
#import "UIViewController+FY.h"
#import "FYMacro.h"
#import "UIScreen+FY.h"

CGFloat const HSYAlertViewLableDistance = 10;

@interface HSYAlertView ()

@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) HSYAttributedString *messege;
@property (nonatomic, strong) NSMutableArray *messeges;
@property (nonatomic, copy) NSString *cancel;
@property (nonatomic, copy) NSString *confirm;
@property (nonatomic, weak) id<HSYAlertViewDelegate> delegate;

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIView *buttonView;
@property (nonatomic, strong) UIView *messegeView;

@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UILabel *messegeLable;
@property (nonatomic, strong) NSMutableArray *messegeLables;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *confirmButton;

@property (nonatomic, assign) CGFloat containerW;
@property (nonatomic, assign) CGFloat containerH;
@property (nonatomic, assign) CGFloat titleViewW;
@property (nonatomic, assign) CGFloat titleViewH;
@property (nonatomic, assign) CGFloat messegeViewW;
@property (nonatomic, assign) CGFloat messegeViewH;
@property (nonatomic, assign) CGFloat buttonViewW;
@property (nonatomic, assign) CGFloat buttonViewH;
@property (nonatomic, assign) CGFloat buttonW;
@property (nonatomic, assign) CGFloat buttonH;

@end

@implementation HSYAlertView

- (instancetype)initWithTitle:(NSString*)title
                       cancel:(NSString*)cancel
                      confirm:(NSString*)confirm
                     delegate:(id<HSYAlertViewDelegate>)delegate
                      messege:(HSYAttributedString*)messege {
    if (self = [super initWithFrame:[[UIScreen mainScreen] bounds]]) {
        self.title = title;
        self.messege = messege;
        self.cancel = cancel;
        self.confirm = confirm;
        self.delegate = delegate;
        [self initViews];
        [self initMessegeLable];
        [self measureFrames];
        [self setupContainerView];
        [self setupTitleView];
        [self setupMessegeLabel];
        [self setupButtonView];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString*)title
                       cancel:(NSString*)cancel
                      confirm:(NSString*)confirm
                     delegate:(id<HSYAlertViewDelegate>)delegate
                     messeges:(HSYAttributedString*)messege, ... {
    if (self = [super initWithFrame:[[UIScreen mainScreen] bounds]]) {
        self.title = title;
        self.cancel = cancel;
        self.confirm = confirm;
        self.delegate = delegate;
        self.messeges = [[NSMutableArray alloc] initWithCapacity:3];
        va_list arguments;
        id eachObject;
        if (messege) {
            [self.messeges addObject:messege];
            va_start(arguments, messege);
            while ((eachObject = va_arg(arguments, id))) {
                [self.messeges addObject:eachObject];
            }
            va_end(arguments);
        }
        
        [self initViews];
        [self initMessegeLables];
        [self measureFrames];
        [self setupContainerView];
        [self setupTitleView];
        [self setupMessegeLables];
        [self setupButtonView];
    }
    return self;
}

- (void)initViews {
    self.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.65];
    self.layer.zPosition = MAXFLOAT;
    
    if (FYIsPad) {
        self.containerW = [UIScreen screenShortSide] * 0.50;
    } else {
        self.containerW = [UIScreen screenShortSide] * 0.90;
    }
    
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.containerView.backgroundColor = [UIColor whiteColor];
    self.containerView.layer.masksToBounds = YES;
    [self addSubview:self.containerView];
    
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.titleView.backgroundColor = [UIColor colorWithHexString:@"F15757" alpha:1.0];
    [self.containerView addSubview:self.titleView];
    
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.titleLable.backgroundColor = [UIColor clearColor];
    [self.titleView addSubview:self.titleLable];
    
    self.messegeView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.messegeView.backgroundColor = [UIColor clearColor];
    [self.containerView addSubview:self.messegeView];
    
    self.buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.buttonView.backgroundColor = [UIColor clearColor];
    [self.containerView addSubview:self.buttonView];
    
    self.cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.cancelButton.layer.masksToBounds = YES;
    [self.cancelButton setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"56B8B8" alpha:1.0]] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonView addSubview:self.cancelButton];
    
    self.confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.confirmButton.layer.masksToBounds = YES;
    [self.confirmButton setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"F15757" alpha:1.0]] forState:UIControlStateNormal];
    [self.confirmButton addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonView addSubview:self.confirmButton];
}

- (void)initMessegeLable {
    CGFloat lableW = self.containerW * 0.85;
    
    self.messegeLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self.messegeView addSubview:self.messegeLable];
    
    self.messegeLable.text = self.messege.string;
    self.messegeLable.textColor = self.messege.color;
    if (self.messege.align == HSYAttributedStringAlignLeft) {
        self.messegeLable.textAlignment = NSTextAlignmentLeft;
    } else if (self.messege.align == HSYAttributedStringAlignCenter) {
        self.messegeLable.textAlignment = NSTextAlignmentCenter;
    } else {
        self.messegeLable.textAlignment = NSTextAlignmentRight;
    }
    self.messegeLable.font = self.messege.font;
    CGRect rect = [self.messegeLable.text boundingRectWithSize:CGSizeMake(lableW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: self.messegeLable.font} context:nil];
    self.messegeLable.numberOfLines = 0;
    self.messegeLable.frameSize = CGSizeMake(rect.size.width, rect.size.height);
    
    self.messegeViewW = self.containerW;
    if (self.messegeLable.frameHeight > self.containerW * 0.3) {
        self.messegeViewH = self.messegeLable.frameHeight;
    } else {
        self.messegeViewH = self.containerW * 0.3;
    }
}

- (void)initMessegeLables {
    self.messegeViewW = self.containerW;
    self.messegeLables = [[NSMutableArray alloc] initWithCapacity:3];
    CGFloat tempY = 0;
    for (HSYAttributedString *messege in self.messeges) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self.messegeView addSubview:lable];
        lable.backgroundColor = [UIColor clearColor];
        lable.text = messege.string;
        lable.textColor = messege.color;
        lable.textAlignment = NSTextAlignmentLeft;
        lable.font = messege.font;
        lable.numberOfLines = 1;
        [lable sizeToFit];
        
        if (messege.align == HSYAttributedStringAlignLeft) {
            lable.frameOriginX = self.messegeViewW * 0.05;
        } else if (messege.align == HSYAttributedStringAlignCenter) {
            lable.frameOriginX = (self.messegeViewW - lable.frameWidth) / 2;
        } else {
            lable.frameOriginX = self.messegeViewW * 0.95 - lable.frameWidth;
        }
        
        tempY = tempY + HSYAlertViewLableDistance;
        lable.frameOriginY = tempY;
        tempY = tempY + lable.frameHeight;
    }
    self.messegeViewH = tempY + HSYAlertViewLableDistance;
}

- (void)measureFrames {
    self.titleViewW = self.containerW;
    self.titleViewH = self.containerW * 0.11;
    
    self.buttonViewW = self.containerW;
    self.buttonViewH = self.containerW * 0.13;
    
    if (self.cancel && self.confirm) {
        self.buttonW = self.buttonViewW * 0.3;
    } else {
        self.buttonW = self.buttonViewW * 0.5;
    }
    self.buttonH = self.buttonViewH * 0.7;
    
    CGFloat tempH = self.titleViewH + self.messegeViewH + self.buttonViewH;
    if (tempH > self.containerH) {
        self.containerH = tempH;
    } else {
        //不做处理
    }
}

- (void)setupContainerView {
    self.containerView.frame = CGRectMake(0, 0, self.containerW, self.containerH);
    self.containerView.center = CGPointMake(self.frameWidth / 2, self.frameHeight / 2);
    self.containerView.layer.cornerRadius = self.containerW * 0.03;
}

- (void)setupTitleView {
    self.titleView.frame = CGRectMake(0, 0, self.titleViewW, self.titleViewH);
    
    self.titleLable.text = self.title;
    self.titleLable.textColor = [UIColor whiteColor];
    self.titleLable.textAlignment = NSTextAlignmentLeft;
    self.titleLable.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:20];
    [self.titleLable sizeToFit];
    
    self.titleLable.center = CGPointMake(self.titleViewW / 2, self.titleViewH / 2);
}

- (void)setupMessegeLabel {
    self.messegeView.frame = CGRectMake(0, self.titleViewH, self.messegeViewW, self.messegeViewH);
    self.messegeLable.center = CGPointMake(self.messegeViewW / 2, self.messegeViewH / 2);
}

- (void)setupMessegeLables {
    self.messegeView.frame = CGRectMake(0, self.titleViewH, self.messegeViewW, self.messegeViewH);
}

- (void)setupButtonView {
    self.buttonView.frame = CGRectMake(0, self.containerH - self.buttonViewH, self.buttonViewW, self.buttonViewH);
    
    self.cancelButton.layer.cornerRadius = self.buttonViewW * 0.02;
    self.confirmButton.layer.cornerRadius = self.buttonViewW * 0.02;
    if (self.cancel && self.confirm) {
        self.cancelButton.frameSize = CGSizeMake(self.buttonW, self.buttonH);
        self.cancelButton.center = CGPointMake(self.buttonViewW * 0.3, self.buttonViewH / 2);
        [self.cancelButton setTitle:self.cancel forState:UIControlStateNormal];
        
        self.confirmButton.frameSize = CGSizeMake(self.buttonW, self.buttonH);
        self.confirmButton.center = CGPointMake(self.buttonViewW * 0.7, self.buttonViewH / 2);
        [self.confirmButton setTitle:self.confirm forState:UIControlStateNormal];
        return;
    }
    if (self.cancel) {
        self.cancelButton.frameSize = CGSizeMake(self.buttonW, self.buttonH);
        self.cancelButton.center = CGPointMake(self.buttonViewW * 0.5, self.buttonViewH / 2);
        [self.cancelButton setTitle:self.cancel forState:UIControlStateNormal];
        return;
    }
    if (self.confirm) {
        self.confirmButton.frameSize = CGSizeMake(self.buttonW, self.buttonH);
        self.confirmButton.center = CGPointMake(self.buttonViewW * 0.5, self.buttonViewH / 2);
        [self.confirmButton setTitle:self.confirm forState:UIControlStateNormal];
        return;
    }
}

- (void)cancelAction:(id)sender {
    [self hideWithComplete:^{
        [self.delegate alertView:self didDismissWithButtonType:HSYAlertViewCancel];
    }];
}

- (void)confirmAction:(id)sender {
    [self hideWithComplete:^{
        [self.delegate alertView:self didDismissWithButtonType:HSYAlertViewConfirm];
    }];
}

- (void)show {
    [[UIViewController currentViewController].view addSubview:self];
}

- (void)hideWithComplete:(void(^)())complete {
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        complete();
    }];
}

@end
